const { ccclass, property } = cc._decorator;
declare let i18n,DBManager,dbh;
import BaseView from "../../component/BaseView";
import UIHelper = require("../../component/UIHelper");
import PopUIHelper = require("../../utils/PopUIHelper");
import Utils = require("../../utils/Utils");
//帮助情侣见面 关卡12
@ccclass
export default class DeputyHelpLoversView extends BaseView {
    @property(cc.Label)
    lblStep: cc.Label = null;
    @property(cc.Label)
    lblDes: cc.Label = null;

    @property(sp.Skeleton)
    spine:sp.Skeleton = null;

    @property(cc.Node)
    road1:cc.Node = null;
    @property(cc.Node)
    road2:cc.Node = null;
    @property(cc.Node)
    road3:cc.Node = null;

    //恭喜获得
    @property(cc.Node)
    endNode: cc.Node = null;

    _done1 = false;
    _dic1 = 1;
    _done2 = false;
    _dic2 = 1;
    _done3 = false;
    _dic3 = 1;

    _isStart = false;
    onLoad() {
        Utils.audioManager.playBGM("huazhuang/deputyGame/deputyGameBg");
        this.road1.on(cc.Node.EventType.TOUCH_START, this.onTouchStart1, this);
        this.road1.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove1, this);
        this.road2.on(cc.Node.EventType.TOUCH_START, this.onTouchStart2, this);
        this.road2.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove2, this);
        this.road3.on(cc.Node.EventType.TOUCH_START, this.onTouchStart3, this);
        this.road3.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove3, this);
        //@ts-ignore
        this._isStart = this.node.openParam;
        this.lblStep.string = i18n.t("DEPUTY_GAME_STEP_TITLE12");
        this.lblDes.string = i18n.t("DEPUTY_GAME_STEP_DES12");
    };
    onClose(){
        Utils.audioManager.playDefaultBGM();
    };
    onTouchStart1(event){
        if(this._done1)
            return;
        let worldPoint = event.touch.getLocation();
        let localPoint = this.road1.parent.convertToNodeSpaceAR(worldPoint);
        if(localPoint.y > this.road1.position.y && (this.road1.angle > -90 && this.road1.angle < 90)){
            this._dic1 = 1;
        }else{
            this._dic1 = 2;
        }
    };  

    onTouchMove1(event){
        if(this._done1)
            return;
        let worldPoint = event.touch.getLocation();
        let localPoint = this.road1.parent.convertToNodeSpaceAR(worldPoint);
        let boyPos = this.road1.position;
        let angle = Utils.utils.getAngle(localPoint,boyPos);
        if(this._dic1 == 1){
            this.road1.angle = angle + 90;
        }else{
            this.road1.angle = angle - 90;
        }
        if ((this.road1.angle > 75 && this.road1.angle < 85) ||
            (this.road1.angle > -275 && this.road1.angle < -265)) {
            this._done1 = true;
            this.road1.angle = 90;
            this.checkDone();
        }
    };
    
    onTouchStart2(event){
        if(this._done2)
            return;
        let worldPoint = event.touch.getLocation();
        let localPoint = this.road2.parent.convertToNodeSpaceAR(worldPoint);
        if(localPoint.y > this.road2.position.y && (this.road2.angle > -90 && this.road2.angle < 90)){
            this._dic2 = 1;
        }else{
            this._dic2 = 2;
        }
    };  

    onTouchMove2(event){
        if(this._done2)
            return;
        let worldPoint = event.touch.getLocation();
        let localPoint = this.road2.parent.convertToNodeSpaceAR(worldPoint);
        let boyPos = this.road2.position;
        let angle = Utils.utils.getAngle(localPoint,boyPos);
        if(this._dic2 == 1){
            this.road2.angle = angle + 90;
        }else{
            this.road2.angle = angle - 90;
        }
        if ((this.road2.angle > 75 && this.road2.angle < 85) ||
            (this.road2.angle > -275 && this.road2.angle < -265)) {
            this._done2 = true;
            this.road2.angle = 90;
            this.checkDone();
        }
    };

    onTouchStart3(event){
        if(this._done3)
            return;
        let worldPoint = event.touch.getLocation();
        let localPoint = this.road3.parent.convertToNodeSpaceAR(worldPoint);
        if(localPoint.y > this.road3.position.y && (this.road3.angle > -90 && this.road3.angle < 90)){
            this._dic3 = 1;
        }else{
            this._dic3 = 2;
        }
    };  

    onTouchMove3(event){
        if(this._done3)
            return;
        let worldPoint = event.touch.getLocation();
        let localPoint = this.road3.parent.convertToNodeSpaceAR(worldPoint);
        let boyPos = this.road3.position;
        let angle = Utils.utils.getAngle(localPoint,boyPos);
        if(this._dic3 == 1){
            this.road3.angle = angle + 90;
        }else{
            this.road3.angle = angle - 90;
        }
        if ((this.road3.angle > 75 && this.road3.angle < 85) ||
            (this.road3.angle > -275 && this.road3.angle < -265)) {
            this._done3 = true;
            this.road3.angle = 90;
            this.checkDone();
        }
    };

    checkDone(){
        if(this._done1 && this._done2 && this._done3){
            this.spine.setAnimation(0,"correct",false);
            this.spine.setCompleteListener(()=>{
                this.endNode.active = true;
                Utils.audioManager.playSound("huazhuang/deputyGame/pass");
            });
        }
    };

    onClickEnd(isSkip){
        let next = this.params.next && this.params.next.shift();
        if (next) {
            UIHelper.openUI(next, {
                next: this.params.next
            })
            this.node.destroy();
        }
    };
    
    onClickSkip(){
        this.onClickEnd(true);
    };

    //提示
    onClickTips(){
        PopUIHelper.helper.showNewConfirmMsg(i18n.t("DEPUTY_GAME_TIPS12"));
    };
}
